/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.gui;

import com.camachosanchez.bean.Flotillas;
import com.camachosanchez.bean.TiposEje;
import com.camachosanchez.bean.TiposVehiculo;
import com.camachosanchez.controler.jdbc.TipoVehiculoJdbcControler;
import com.camachosanchez.controler.jpa.FlotillasJpaController;
import com.camachosanchez.reportes.ReporteBajaNeumaticos;
import com.camachosanchez.util.GUIUtil;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author ZULLY
 */
public class DialogoReporteBajasNeumatico extends javax.swing.JDialog {

    /**
     * Creates new form DialogoReporteBajasNeumatico
     */
    public DialogoReporteBajasNeumatico(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        pack();
        setLocationRelativeTo(null);
        GUIUtil.addEscapeKey(this);
        cargarCombos();
        Calendar c = new GregorianCalendar();
        int annio = c.get(Calendar.YEAR);
        selectorAnio.setEndYear(annio);
    }

    private void cargarCombos() {
        FlotillasJpaController flotillas_dao = new FlotillasJpaController(null);
        List<Flotillas> flotillas = flotillas_dao.findFlotillasEntities();
        cmbEmpresas.setModel(new DefaultComboBoxModel(flotillas.toArray()));

        TipoVehiculoJdbcControler tipos_vehiculo_dao = new TipoVehiculoJdbcControler();
        List<TiposVehiculo> tipos_vehiculos = tipos_vehiculo_dao.findAll();
        cmbTipoUnidad2.setModel(new DefaultComboBoxModel(tipos_vehiculos.toArray()));

        TiposVehiculo tipoSeleccionado = (TiposVehiculo) cmbTipoUnidad2.getSelectedItem();
        List<TiposEje> ejes = tipoSeleccionado.getTiposEjeList();
        List<TiposEje> ejesActivos = new ArrayList<TiposEje>();
        for(TiposEje item : ejes){
            if(item.getActivo()){
                ejesActivos.add(item);
            }
        }
        cmbModelo.setModel(new DefaultComboBoxModel(ejesActivos.toArray()));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlContenedor = new javax.swing.JPanel();
        lblRazonSocial = new javax.swing.JLabel();
        lblMes = new javax.swing.JLabel();
        lblAnio = new javax.swing.JLabel();
        selectorAnio = new com.toedter.calendar.JYearChooser();
        selectorMes = new com.toedter.calendar.JMonthChooser();
        cmbEmpresas = new javax.swing.JComboBox();
        lblTipoUnidad = new javax.swing.JLabel();
        cmbTipoUnidad2 = new javax.swing.JComboBox();
        lblNombreModelo = new javax.swing.JLabel();
        cmbModelo = new javax.swing.JComboBox();
        barraHerramientas = new javax.swing.JToolBar();
        pnlHerramientas = new javax.swing.JPanel();
        btnImprimir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(".:: Próximos Desmontajes de Neumáticos ::.");
        setResizable(false);

        pnlContenedor.setBackground(new java.awt.Color(231, 239, 247));

        lblRazonSocial.setText("Razón Social:");

        lblMes.setText("Mes:");

        lblAnio.setText("Año:");

        selectorAnio.setPreferredSize(new java.awt.Dimension(47, 23));

        selectorMes.setPreferredSize(new java.awt.Dimension(99, 23));

        cmbEmpresas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbEmpresas.setPreferredSize(new java.awt.Dimension(56, 23));

        lblTipoUnidad.setText("Tipo de unidad:");

        cmbTipoUnidad2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbTipoUnidad2.setPreferredSize(new java.awt.Dimension(56, 23));
        cmbTipoUnidad2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbTipoUnidad2ActionPerformed(evt);
            }
        });

        lblNombreModelo.setText("Modelo:");

        cmbModelo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbModelo.setPreferredSize(new java.awt.Dimension(56, 23));

        javax.swing.GroupLayout pnlContenedorLayout = new javax.swing.GroupLayout(pnlContenedor);
        pnlContenedor.setLayout(pnlContenedorLayout);
        pnlContenedorLayout.setHorizontalGroup(
            pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlContenedorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlContenedorLayout.createSequentialGroup()
                        .addComponent(lblRazonSocial)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbEmpresas, 0, 279, Short.MAX_VALUE))
                    .addGroup(pnlContenedorLayout.createSequentialGroup()
                        .addGroup(pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(pnlContenedorLayout.createSequentialGroup()
                                .addComponent(lblTipoUnidad)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cmbTipoUnidad2, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlContenedorLayout.createSequentialGroup()
                                .addComponent(lblMes)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(selectorMes, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(4, 4, 4)
                        .addGroup(pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblAnio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblNombreModelo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(selectorAnio, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(cmbModelo, javax.swing.GroupLayout.Alignment.LEADING, 0, 134, Short.MAX_VALUE))))
                .addContainerGap())
        );

        pnlContenedorLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lblMes, lblRazonSocial, lblTipoUnidad});

        pnlContenedorLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lblAnio, lblNombreModelo});

        pnlContenedorLayout.setVerticalGroup(
            pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlContenedorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbEmpresas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRazonSocial))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTipoUnidad)
                    .addComponent(cmbTipoUnidad2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombreModelo)
                    .addComponent(cmbModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(selectorMes, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                    .addComponent(selectorAnio, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                    .addComponent(lblMes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblAnio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(pnlContenedor, java.awt.BorderLayout.CENTER);

        barraHerramientas.setBackground(new java.awt.Color(255, 255, 255));
        barraHerramientas.setBorder(null);
        barraHerramientas.setFloatable(false);
        barraHerramientas.setRollover(true);
        barraHerramientas.setMaximumSize(new java.awt.Dimension(65, 30));
        barraHerramientas.setMinimumSize(new java.awt.Dimension(0, 30));
        barraHerramientas.setPreferredSize(new java.awt.Dimension(65, 30));

        pnlHerramientas.setBackground(new java.awt.Color(255, 255, 255));
        pnlHerramientas.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlHerramientas.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 2, 3));

        btnImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/camachosanchez/recursos/pdf.png"))); // NOI18N
        btnImprimir.setText("Imprimir");
        btnImprimir.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnImprimir.setContentAreaFilled(false);
        btnImprimir.setFocusable(false);
        btnImprimir.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirActionPerformed(evt);
            }
        });
        pnlHerramientas.add(btnImprimir);

        barraHerramientas.add(pnlHerramientas);

        getContentPane().add(barraHerramientas, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbTipoUnidad2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbTipoUnidad2ActionPerformed
        // TODO add your handling code here:
        TiposVehiculo tipoSeleccionado = (TiposVehiculo) cmbTipoUnidad2.getSelectedItem();
        List<TiposEje> ejes = tipoSeleccionado.getTiposEjeList();
        if (!ejes.isEmpty()) {
            cmbModelo.setModel(new DefaultComboBoxModel(ejes.toArray()));
        } else {
            cmbModelo.setModel(new DefaultComboBoxModel());
        }
    }//GEN-LAST:event_cmbTipoUnidad2ActionPerformed

private void btnImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirActionPerformed
// TODO add your handling code here:
    imprimir();
}//GEN-LAST:event_btnImprimirActionPerformed

    private void imprimir() {
        final DialogoCargando dlg = new DialogoCargando(Principal.getInstance(), true);
        Thread hilo = new Thread() {

            @Override
            public void run() {
                try {
                    ReporteBajaNeumaticos reporteR = new ReporteBajaNeumaticos();
                    Calendar fechaIni = new GregorianCalendar(selectorAnio.getYear(), selectorMes.getMonth(), 1);
                    Calendar fechaFin = new GregorianCalendar(selectorAnio.getYear(), selectorMes.getMonth(), fechaIni.getActualMaximum(Calendar.DAY_OF_MONTH));
                    if (cmbEmpresas.getSelectedItem() != null && cmbModelo.getSelectedItem() != null) {
                        Flotillas razonSocial = (Flotillas) cmbEmpresas.getSelectedItem();
                        TiposEje tipoEje = (TiposEje) cmbModelo.getSelectedItem();
                        TiposVehiculo tipoUnidad = (TiposVehiculo) cmbTipoUnidad2.getSelectedItem();
                        reporteR.generarReporte(fechaIni.getTime(), fechaFin.getTime(), razonSocial, tipoEje, tipoUnidad);
                    } else {
                        JOptionPane.showMessageDialog(Principal.getInstance(), "Seleccione los datos completos para el reporte.", "Datos incompletos", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    dlg.dispose();
                }
            }
        };
        hilo.start();
        dlg.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar barraHerramientas;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JComboBox cmbEmpresas;
    private javax.swing.JComboBox cmbModelo;
    private javax.swing.JComboBox cmbTipoUnidad2;
    private javax.swing.JLabel lblAnio;
    private javax.swing.JLabel lblMes;
    private javax.swing.JLabel lblNombreModelo;
    private javax.swing.JLabel lblRazonSocial;
    private javax.swing.JLabel lblTipoUnidad;
    private javax.swing.JPanel pnlContenedor;
    private javax.swing.JPanel pnlHerramientas;
    private com.toedter.calendar.JYearChooser selectorAnio;
    private com.toedter.calendar.JMonthChooser selectorMes;
    // End of variables declaration//GEN-END:variables
}
